//
//  SNHBaseSideActionSheet.m
//  SNHActionSheetKit
//
//  Created by 陈建蕾 on 2018/8/30.
//

#import "SNHBaseSideActionSheet.h"
#import <SNHActionSheetKit/SNHFeedbackGenerator.h>

@interface SNHBaseSideActionSheet ()
@property (nonatomic, assign) CGFloat containHeight;
@end

@implementation SNHBaseSideActionSheet

+ (instancetype)actionSheetWithHeight:(CGFloat)height {
    return [[self alloc] initWithHeight:height];
}

- (instancetype)initWithHeight:(CGFloat)height {
    if (self = [super initWithFrame:CGRectZero]) {
        _containHeight = height;
        [self _config];
        [self viewDidLoad];
    }
    return self;
}

#pragma mark - Private
- (void)_config {
    _startDuration = _endDuration = 0.37f;
    _startDelay = _endDelay = 0.f;
    _startDamping =  _endDamping = 1.f;
    _startVelocity = _endVelocity = 0.f;
    _startOptions = _endOptions = UIViewAnimationOptionCurveEaseIn;
    _isTapMiss = YES;
}

- (void)viewDidLoad {
    self.actionSheetView = [[UIView alloc]initWithFrame:CGRectZero];
    self.actionSheetView.opaque = YES;
    self.actionSheetView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.actionSheetView];
}

#pragma mark - public
- (void)showInWindow {
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [SNHFeedbackGenerator light];
}

- (void)showInSuperView:(UIView *)superView {
    if (superView) {
        [superView addSubview:self];
        [SNHFeedbackGenerator light];
    }
}

- (void)miss {
    [UIView animateWithDuration:self.endDuration delay:self.endDelay usingSpringWithDamping:self.endDamping initialSpringVelocity:self.endVelocity options:self.endOptions animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.actionSheetView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), 0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - System
- (void)didMoveToSuperview {
    if (self.superview) {
        self.frame = self.superview.bounds;
        
        if ([self.superview isKindOfClass:UIWindow.class]) {
            if (@available(iOS 11.0, *)) {
                UIEdgeInsets safeEdge = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
                self.containHeight += safeEdge.bottom;
            }
        }
        
        self.actionSheetView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), 0);
        
        [UIView animateWithDuration:self.startDuration delay:self.startDelay usingSpringWithDamping:self.startDamping initialSpringVelocity:self.startVelocity options:self.startOptions animations:^{
            self.actionSheetView.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - self.containHeight, CGRectGetWidth(self.bounds), self.containHeight);
        } completion:^(BOOL finished) {
            [super didMoveToSuperview];
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    if (!CGRectContainsPoint(self.actionSheetView.frame, touchPoint)) {
        if (self.isTapMiss) {
            [self miss];
        }
    }
}

@end
